package com.gpj.sqlbuilder.store.sql;

import com.gpj.sqlbuilder.annotations.ExcelOrder;
import com.gpj.sqlbuilder.enums.ColumnType;
import com.gpj.sqlbuilder.enums.PrimaryType;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

/**
 * @className Column
 * @description TODO
 * @author GPJ
 * @date 2020/3/18 16:47
 * @version 1.0
 **/
@Setter
@Getter
@NoArgsConstructor
public class Column {
    // 表名
    @ExcelOrder(value = 0, required = true)
    private String tableName;

    // 字段名称
    @ExcelOrder(value = 1, required = true)
    private String name;

    // 字段中文名称
    @ExcelOrder(value = 8, required = true)
    private String note;

    // 是否必填
    @ExcelOrder(2)
    private boolean required = false;

    // 是否为主键
    @ExcelOrder(value = 3)
    private PrimaryType primaryType = PrimaryType.NONE;

    // 类型
    @ExcelOrder(value = 4, required = true)
    private ColumnType type;

    // 长度
    @ExcelOrder(5)
    private int length = -1;

    // 精度
    @ExcelOrder(6)
    private int accuracy = -1;

    // 默认值
    @ExcelOrder(7)
    private String defaultValue = "";

    // 编码
    @ExcelOrder(11)
    private String encod = "";

    // 字典值
    @ExcelOrder(value = 9)
    private String dict = "";

    private String attribute;

    public boolean isPrimary() {
        return primaryType != PrimaryType.NONE;
    }

    public boolean isStringValue(){
        return type == ColumnType.C || type == ColumnType.UUID
                || type == ColumnType.VC || type == ColumnType.T || type == ColumnType.DT;
    }

    public boolean isRequired() {
        return required || isPrimary();
    }

    public String getDefaultValue() {
        if(StringUtils.isNotBlank(defaultValue)) return defaultValue;
        if(isPrimary() || isRequired()) return "";
        switch (type) {
            case ID: case N: case I:
                return "0";
            case UUID: case C: case VC: case T: case D: case DT: case BLOB: case CLOB:
            default:
                return "NULL";
        }
    }
}